London | 26-ITP-May | Martin Mwaka | Sprint 1 | Exercises#1234
Conversation
LonMcGregor
left a comment
There was a problem hiding this comment.
Good work on these tasks, there are a couple where I have some questions to check your ideas behind what you've written
|
|
||
| // filter only numbers from array and sort the filtered array. If no numbers return null | ||
| const numbers = list | ||
| .filter((item) => typeof item === "number" && Number.isFinite(item)) |
There was a problem hiding this comment.
Why are you checking isFinite here?
There was a problem hiding this comment.
I don't need isFinite here because by this stage non numbers have been filtered out. I have removed it.
| // return empty array if empty input | ||
| if (array.length === 0) return []; | ||
| // check if array has duplicates | ||
| const hasDuplicates = new Set(array).size !== array.length; |
There was a problem hiding this comment.
Do you know how a set works? Is it necessary to check for hasDuplicated here?
There was a problem hiding this comment.
Yes, on review, I can just return the set as it will either remove duplicates or give me the original array. I have refactored this to a single line
| // filter out the numbers and sum them up | ||
| return elements | ||
| .filter((item) => Number.isFinite(item)) | ||
| .reduce((acc, num) => acc + num, 0); |
There was a problem hiding this comment.
Good use of chaining higher order functions together
Self checklist
Changelist
I have completed all exercises, including stretch, in sprint 1